//
//  BDCityInfo.m
//  BadBoy
//
//  Created by wen on 2017/2/22.
//  Copyright © 2017年 niusaibing. All rights reserved.
//

#import "BDCityInfo.h"

@implementation BDCityInfo

+ (NSDictionary *)mj_replacedKeyFromPropertyName {
    return @{@"infoID":@"id"};
}

/*
 AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
 if ([NSStringFromClass(obClass) isEqualToString:@"BDCityInfo"] && appdelegate.cityArray != nil) {
 NSArray *conditionArray = [condition componentsSeparatedByString:@" and "];
 NSMutableDictionary *conditionDiction = [NSMutableDictionary dictionary];
 for (NSString *litCond in conditionArray) {
 NSArray *litttArr = [litCond componentsSeparatedByString:@" "];
 //            NSLog(ASLocalizedString(@"key是%@"), litttArr[0]);
 NSString *valueStr = litttArr[2];
 //            NSLog(ASLocalizedString(@"Value是%@"), [[valueStr substringToIndex:valueStr.length - 1] substringFromIndex:1]);
 [conditionDiction setObject:[[valueStr substringToIndex:valueStr.length - 1] substringFromIndex:1] forKey:litttArr[0]];
 }
 NSMutableArray *resultArr = [NSMutableArray array];
 for (BDCityInfo *cityInfo in appdelegate.cityArray) {
 NSDictionary *dic = cityInfo.mj_keyValues;
 BOOL flag = YES;
 for (NSString *key in conditionDiction.allKeys) {
 NSString *newDicKey = key;
 if ([key isEqualToString:@"infoID"]) {
 newDicKey = @"id";
 }
 if (![conditionDiction[key] isEqualToString:dic[newDicKey]]) {
 flag = NO;
 break;
 }
 }
 if (flag == YES) {
 [resultArr addObject:cityInfo];
 }
 }
 return resultArr;
 }
 */

+ (NSArray *)getCityArray{
    NSString *path = [DocumentPath stringByAppendingPathComponent:cityListPath];
    return [BDCityInfo mj_objectArrayWithFile:path];
}

+ (BDCityInfo *)getCityWithName:(NSString *)cityName{
    NSArray *cityArray = [self getCityArray];
    
    for (BDCityInfo *cityInfo in cityArray) {
        if ([cityInfo.name isEqualToString:cityName]) {
            return cityInfo;
        }
    }
    return nil;
}

+ (BDCityInfo *)getCityWithName:(NSString *)cityName type:(BDCityType)cityType{
    NSArray *cityArray = [self getCityArray];
    
    for (BDCityInfo *cityInfo in cityArray) {
        if ([cityInfo.name isEqualToString:cityName] && cityInfo.type == cityType) {
            return cityInfo;
        }
    }
    return nil;
}

+ (BDCityInfo *)getCityWithId:(NSInteger)cityId{
    NSArray *cityArray = [self getCityArray];
    
    for (BDCityInfo *cityInfo in cityArray) {
        if (cityInfo.infoID == cityId) {
            return cityInfo;
        }
    }
    return nil;
}

+ (NSArray *)getCityArrayWithType:(BDCityType)cityType{
    NSArray *cityArray = [self getCityArray];
    NSMutableArray *resArray = [NSMutableArray array];
    for (BDCityInfo *cityInfo in cityArray) {
        if (cityInfo.type == cityType) {
            [resArray addObject:cityInfo];
        }
    }
    return resArray;
}

+ (NSArray *)getCityArrayWithParentId:(NSInteger)parentId{
    NSArray *cityArray = [self getCityArray];
    NSMutableArray *resArray = [NSMutableArray array];
    for (BDCityInfo *cityInfo in cityArray) {
        if (cityInfo.parent_id == parentId) {
            [resArray addObject:cityInfo];
        }
    }
    return resArray;
}

@end
